<?php
/**
 * Klasa służąca do budowy elementów html
 *
 * @author Lukas
 */
class Html {
  private $newLine = "\n";

  /**
   *Funkcja budująca pojedyńczy tag HTML w oparciu o następuące parametry
   *
   * @param <string> $tag         - tag HTML
   * @param <string> $tagName     - nazwa która ma być nadana tagowi, z niej buduje się również id z dopiskiem _id, moze byc puste
   * @param <string> $tagClass    - nazwa klasy css ktora ma byc urzyta, moze byc puste
   * @param <array> $addAtr       - tablica z tablicami dodatkowych atrybutow array( array("<nawa_atryb>","<wartosc_atrybutu>"),... )
   * @param <string> $tagContent  - zawartość taga
   * @return <string>
   */
  public function tagElementSimple($tag,$tagName="",$tagClass="",$addAtr=array(),$tagContent=""){
    if(!is_array($addAtr))
        $addAtr = array();
    if( $tag == "" ) {
      return "";
    }
    else {
      if( $tagName=="" && $tagClass=="" && count( $addAtr ) == 0 && $tagContent=="" ) {
        $element = "<".$tag."/>".$this->newLine;
      }
      else {
        $element = "<".$tag." ";
        $element .= ( $tagName != "" ? "id=\"".$tagName."_id\" name=\"".$tagName."\" " : "");
        $element .= ( $tagClass != "" ? "class=\"".$tagClass."\" " : "");

        foreach( $addAtr as $k => $atr ) {
          if( count($atr) == 2 ) {
            $element .= $atr[ 0 ]."=\"".$atr[ 1 ]."\" ";
          }
          else if( is_array($atr) && count($atr) == 1 ) {
            $element .= $atr[ 0 ]." ";
          }
          else if(!is_numeric($k) && !empty($atr)){
            $element .= $k."=\"".$atr."\" ";
          }
          else if(!empty($atr)){
            $element .= $atr." ";
          }
        }

        if( $tagContent != "" ) {
          $element .= ">".$this->newLine;
          $element .= trim($tagContent);
          $element .= "</".$tag.">".$this->newLine;
        }
        else{
          $element .= "/>";
        }

      }
    }

    return $element;
  }

  public function arrayIntoHeadlesTable($tableName, $tableClass = "", $arr = array()) {
    $table = "";

    if( $tableName != "" ) {
      $table .= "<table id='".$tableName."_id' name='".$tableName."'";
      $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
      $table .= ">".$this->newLine;

      foreach( $arr as $row ) {
        $table .= "<tr";
        $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
        $table .= ">";
        foreach($row as $kols) {
          $table .= "<td";
          $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
          $table .= ">";
          $table .= $kols;
          $table .= "</td>";
        }
        $table .= "</tr>".$this->newLine;
      }

      $table .= "</table>".$this->newLine;
    }

    return $table;
  }

  /**
   * Metoda zwracająca nagłówek tabeli html w postaci <table><tr><th></th><th></th>...</tr> NIE domknięty znacznikiem </table>
   * @param String $tableName Nazwa bydowanej talbicy - z niej powstanie również id poprzez dodanie "_id"
   * @param array() $tableClasses Tablica klas css w kolejcości array( <klasa_tablicy>,<klasa_tr>,<klasa_th> )
   * @param array() $head Tablica Pozycji nagłówka tablicy
   */
  public function tableHead($tableName, $tableClasses = array(), $head = array()){
    $table = "";

    if( $tableName != "" && count( $head ) > 0 ) {
      $table .= "<table id='".$tableName."_id' name='".$tableName."'";
      $table .= ( $tableClasses[0] != "" ? " class='".$tableClasses[0]."'" : "" );
      $table .= ">".$this->newLine;
      $table .= "<tr";
      $table .= ( $tableClasses[1] != "" ? " class='".$tableClasses[1]."'" : "" );
      $table .= ">";
      foreach( $head as $row ) {
        $table .= "<th";
        $table .= ( $tableClasses[2] != "" ? " class='".$tableClasses[2]."'" : "" );
        $table .=">";
        $table .= $row;
        $table .= "</th>";
      }
      $table .= "</tr>".$this->newLine;

    }

    return $table;
  }

  /**
   *
   * @param string $tableName
   * @param string $tableClass
   * @param array $head
   * @param array $arr
   * @return string
   */

  public function arrayIntoTable($tableName, $tableClass = "", $head = array(), $arr = array()) {
    $table = "";
    
    if( $tableName != "" && count( $arr ) > 0 ) {
      $table .= "<table id='".$tableName."_id' name='".$tableName."'";
      $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
      $table .= ">".$this->newLine;
      $table .= "<tr";
      $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
      $table .= ">";
      foreach( $head as $row ) {
        $table .= "<th";
        $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
        $table .=">";
        $table .= $row;
        $table .= "</th>";
      }
      $table .= "</tr>".$this->newLine;
      foreach( $arr as $row ) {
        $table .= "<tr";
        $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
        $table .= ">";
        $j = count($head);
        $i = 1;
        foreach($row as $kols) {
          if($i<=$j){
            $table .= "<td";
            $table .= ( $tableClass != "" ? " class='".$tableClass."'" : "" );
            $table .=">";
            $table .= ($kols != "" ? $kols : "---" );
            $table .= "</td>";
          }
          $i++;
        }
        $table .= "</tr>".$this->newLine;
      }

      $table .= "</table>".$this->newLine;
    }

    return $table;
  }

  public function brTag(){
    return $this->tagElementSimple( "br", "", "", array(), "" );
  }

  public function tdSimpleTag($content,$array = array()){
      return $this->tagElementSimple( "td", "", "", $array, $content );
  }

    public function liSimpleTag($content){
      return $this->tagElementSimple( "li", "", "", array(), $content );
    }
    public function simpleTag($tag,$content,$attr = array()){
      if(!is_array($attr)){
          $attr = array();
      }
      return $this->tagElementSimple( $tag, "", "", $attr, $content );
  }

  /**
     *
     * @param array $optionArray  według wzoru array("option_value" => "option_content");
     * @param array $attr tak samo jak w simpleTag
     * @param int $selected klucz "option_value" z tabeli $optionArray
     * @return string
     */
    public function getSelectFromArray($optionArray , $attr = array(), $selected = null) {
        $options = "";
        foreach ($optionArray as $key => $value) {
            if($selected!=null && $selected==$key){
                $s = "selected";
            }else{
                $s = null;
            }
            $options .= $this->simpleTag("option", $value, array("value"=>$key,$s=>$s));
        }
        return $this->simpleTag("select", $options , $attr);
    }
}

